package com.lijin.batchsys.framework.dao.model;

import org.apache.commons.lang.StringUtils;

import com.lijin.batchsys.framework.common.BatchSysConstants;
import com.lijin.batchsys.framework.schedule.RunDate;

public class StepInfo {

	private int id;
	private int batchNo;
	private String batchDesc;
	private int jobNo;
	private String jobDesc;
	private int stepNo;
	private String stepDesc;
	private String StepBeanName;
	private String stepRunPeriod; //步骤执行周期
	private String stepCtrlFlgs; //步骤执行控制位
	private char[] ctrlFlgsArr; //步骤控制位数组
	private String chStepRunPeriod; //汉字表达--步骤执行周期
	private String chStepCtrlFlgs; //汉字表达--步骤执行控制位
	private String recSt;
	
	/**
	 * 根据Step的执行周期类型来判断指定的日期是否执行该步骤
	 * @param runDateStr
	 * @return
	 */
	public boolean checkRunDate(String runDateStr) {
		RunDate rd = new RunDate(runDateStr);
		return rd.checkRun(stepRunPeriod);
	}
	
	public String getSummary() {
		StringBuilder desc = new StringBuilder();
		desc.append("[batch:" + this.batchNo + "-" + this.batchDesc + ",");
		desc.append("job:" + this.jobNo + "-" + this.jobDesc + ",");
		desc.append("step:" + this.stepNo + "-" + this.stepDesc + "(" + this.StepBeanName + ")" + "]");
		return desc.toString();
	}
	
	public int getStepNo() {
		return stepNo;
	}
	public void setStepNo(int stepNo) {
		this.stepNo = stepNo;
	}
	public String getStepBeanName() {
		return StepBeanName;
	}
	public void setStepBeanName(String stepBeanName) {
		StepBeanName = stepBeanName;
	}
	public int getJobNo() {
		return jobNo;
	}
	public void setJobNo(int jobNo) {
		this.jobNo = jobNo;
	}
	public int getBatchNo() {
		return batchNo;
	}
	public void setBatchNo(int batchNo) {
		this.batchNo = batchNo;
	}
	public String getStepRunPeriod() {
		return stepRunPeriod;
	}
	public void setStepRunPeriod(String stepRunPeriod) {
		this.stepRunPeriod = stepRunPeriod;
	}
	public String getStepCtrlFlgs() {
		return stepCtrlFlgs;
	}
	public String getChStepCtrlFlgs() {
		return chStepCtrlFlgs;
	}

	public void setChStepCtrlFlgs(String chStepCtrlFlgs) {
		this.chStepCtrlFlgs = chStepCtrlFlgs;
	}
	
	public String getChStepRunPeriod() {
		return chStepRunPeriod;
	}

	public void setChStepRunPeriod(String chStepRunPeriod) {
		this.chStepRunPeriod = chStepRunPeriod;
	}
	
	public void setStepCtrlFlgs(String stepCtrlFlgs) {
		// 补全32位，不足32位则右补0
		this.stepCtrlFlgs = StringUtils.rightPad(stepCtrlFlgs.trim(), 32, '0');
		this.ctrlFlgsArr = this.stepCtrlFlgs.toCharArray();
	}
	
	public boolean isEnabled() {
		return BatchSysConstants.REC_VALID.equals(recSt);
	}
	/**
	 * 步骤执行控制位第1位来控制步骤失败时是否可自动跳过
	 * 1 - 步骤失败时可跳过执行下个步骤--可跳过
	 * 0 - 不可跳过，步骤失败时任务中断--整个任务停止
	 * @return
	 */
	public boolean canSkip() {
		if (ctrlFlgsArr == null) {
			throw new IllegalStateException("控制位数组未初始化");
		}
		return ctrlFlgsArr[0] == '1';
	}
	/**
	 * 步骤执行控制位第2位来控制步骤失败时是否可手工跳过（当第1位为0不可跳过时，本控制位才起作用）
	 * 0 - 不可跳过，当前步骤必须成功才能执行下个步骤
	 * 1 - 步骤失败时，在页面可选择忽略来执行下个步骤
	 * @return
	 */
	public boolean canSkipManually() {
		if (ctrlFlgsArr == null) {
			throw new IllegalStateException("控制位数组未初始化");
		}
		return ctrlFlgsArr[1] == '1';
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getRecSt() {
		return recSt;
	}

	public void setRecSt(String recSt) {
		this.recSt = recSt;
	}

	public String getStepDesc() {
		return stepDesc;
	}

	public void setStepDesc(String stepDesc) {
		this.stepDesc = stepDesc;
	}
	
	public String getBatchDesc() {
		return batchDesc;
	}

	public void setBatchDesc(String batchDesc) {
		this.batchDesc = batchDesc;
	}

	public String getJobDesc() {
		return jobDesc;
	}

	public void setJobDesc(String jobDesc) {
		this.jobDesc = jobDesc;
	}
}
